﻿using System;
using System.Threading;

namespace System.Net.DnsToolkit.SocketAsync {

    internal class SocketStats {

        #region Properties 

        private int _totalSendTo;
        internal int TotalSendTo {
            get {
                return _totalSendTo;
            }
        }

        private int _totalReceiveFrom;
        internal int TotalReceiveFrom {
            get {
                return _totalReceiveFrom;
            }
        }

        private int _totalReceiveTimeout;
        internal int TotalReceiveTimeout {
            get {
                return _totalReceiveTimeout;
            }
        }

        private int _responseSuccess;
        internal int ResponseSuccess {
            get {
                return _responseSuccess;
            }
        }


        private long _startedAt;
        internal DateTime StartedAt {
            get {
                return DateTime.FromBinary(_startedAt);
            }
        }

        #endregion

        #region Ctor

        public SocketStats() {
            ResetStats();
        }

        #endregion

        internal void ResetStats() {
            lock (this) {
                Interlocked.Exchange(ref _totalSendTo, 0);
                Interlocked.Exchange(ref _totalReceiveFrom, 0);
                Interlocked.Exchange(ref _totalReceiveTimeout, 0);
                Interlocked.Exchange(ref _responseSuccess, 0);
               Interlocked.Exchange(ref _startedAt, DateTime.Now.ToBinary());
            }
        }

        internal void IncrementTotalSendTo() {
            Interlocked.Increment(ref _totalSendTo);
        }
        internal void IncrementTotalReceiveFrom() {
            Interlocked.Increment(ref _totalReceiveFrom);
        }
        internal void IncrementTotalReceiveTimeout() {
            Interlocked.Increment(ref _totalReceiveTimeout);
        }
        internal void IncrementResponseSuccess() {
            Interlocked.Increment(ref _responseSuccess);
        }
        
    }
}
